﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using ByteSite.Common;
using SqlSugar;

namespace ByteSite.BLL
{
    /// <summary>
    ///物流快递
    /// </summary>
    public partial class form_data_role
    {
        private readonly Entitys.sysconfig sysConfig = new BLL.sysconfig().loadConfig();//获得系统配置信息
        private readonly DAL.Data data;

        public form_data_role()
        {
            data = new DAL.Data();
        }

        #region 基本方法================================
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            return data.Exists<Entitys.dt_form_data_role>(id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Entitys.dt_form_data_role model)
        {
            try
            {
                data.BeginTran();
                model.id = data.Insert<Entitys.dt_form_data_role>(model);
                if (model.role_value_list != null && model.role_value_list.Count > 0)
                {
                    foreach (Entitys.dt_form_data_role_value v in model.role_value_list)
                    {
                        v.role_id = model.id;
                    }
                    data.Insert(model.role_value_list);
                }
                data.CommitTran();
            }
            catch 
            {
                data.RollbackTran();
                return 0;
            }
            return model.id;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Entitys.dt_form_data_role model)
        {
            try
            {
                data.BeginTran();
                data.Update(model);
                data.Delete<Entitys.dt_form_data_role_value>(x => x.role_id == model.id);
                if (model.role_value_list != null && model.role_value_list.Count > 0)
                {
                    data.Insert(model.role_value_list);
                }

                data.CommitTran();
            }
            catch
            {
                data.RollbackTran();
                return false;
            }
            return true;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            data.Delete<Entitys.dt_form_data_role_value>(x => x.role_id == id);
            return data.Delete<Entitys.dt_form_data_role>(id);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Entitys.dt_form_data_role GetModel(int id)
        {
            Entitys.dt_form_data_role model = data.Get<Entitys.dt_form_data_role>(id);
            model.role_value_list = new List<Entitys.dt_form_data_role_value>();
            model.role_value_list.AddRange(data.List<Entitys.dt_form_data_role_value>(x => x.role_id == id));
            return model;
        }

        /// <summary>
        /// 获得全部数据
        /// </summary>
        public List<Entitys.dt_form_data_role> GetList( Expression<Func<Entitys.dt_form_data_role, bool>> whereExpression, Expression<Func<Entitys.dt_form_data_role, object>> orderExpression)
        {
            List<Entitys.dt_form_data_role> list = data.List<Entitys.dt_form_data_role>(whereExpression, orderExpression);
            List<Entitys.dt_form_data_role> newList = new List<Entitys.dt_form_data_role>();
            foreach (Entitys.dt_form_data_role model in list)
            {
                newList.Add(GetModel(model.id));
            }
            return newList;
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public List<Entitys.dt_form_data_role> GetList(int top, Expression<Func<Entitys.dt_form_data_role, bool>> whereExpression, Expression<Func<Entitys.dt_form_data_role, object>> orderExpression)
        {
            List<Entitys.dt_form_data_role> list = data.List<Entitys.dt_form_data_role>(top, whereExpression, orderExpression);
            List<Entitys.dt_form_data_role> newList = new List<Entitys.dt_form_data_role>();
            foreach (Entitys.dt_form_data_role model in list)
            {
                newList.Add(GetModel(model.id));
            }
            return newList;
        }

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public List<Entitys.dt_form_data_role> GetList(int pageSize, int pageIndex, Expression<Func<Entitys.dt_form_data_role, bool>> whereExpression, Expression<Func<Entitys.dt_form_data_role, object>> orderExpression, out int recordCount)
        {
            List<Entitys.dt_form_data_role> list = data.List<Entitys.dt_form_data_role>(pageSize, pageIndex, whereExpression, orderExpression, out recordCount);
            List<Entitys.dt_form_data_role> newList = new List<Entitys.dt_form_data_role>();
            foreach (Entitys.dt_form_data_role model in list)
            {
                newList.Add(GetModel(model.id));
            }
            return newList;
        }

        /// <summary>
        /// 修改一列数据
        /// </summary>
        public bool UpdateField(Expression<Func<Entitys.dt_form_data_role, Entitys.dt_form_data_role>> expression, Expression<Func<Entitys.dt_form_data_role, bool>> whereExpression)
        {
            return data.UpdateColumns<Entitys.dt_form_data_role>(expression, whereExpression);
        }
        #endregion
    }
}